using System.Collections.Generic;
using Lucene.Net.Search;
using Lucene.Net.Documents;
using NopSolutions.NopCommerce.BusinessLogic.Products;
using NopSolutions.NopCommerce.BusinessLogic.Products.Specs;

namespace NopSolutions.NopCommerce.LuceneHelper
{
    public class ProductSearchResult
    {
        private SearchManager _searchManager;
        private IList<Document> _productDocuments = new List<Document>();
        private int _total;
        private int _start;
        private int _end;
        private Query _query;
        private IList<SpecificationAttributeOption> _facets = null;
        private FilterContext _filterContext;
        private DocIdSet _docIdSet = null;

        public ProductSearchResult(int start, int end, int total, Query query, FilterContext filterContext, IList<Document> productDocuments, SearchManager searchManager)
        {
            _start = start;
            _end = end;
            _total = total;
            _query = query;
            _productDocuments = productDocuments;
            _searchManager = searchManager;
            _filterContext = filterContext;
        }

        public int Total
        {
            get { return _total; }
        }

        public int Start
        {
            get { return _start; }
        }

        public int End
        {
            get { return _end; }
        }

        public Query Query
        {
            get { return _query; }
        }

        public DocIdSet DocIdSet
        {
            get
            {
                if(_docIdSet == null)
                {
                    _docIdSet = _searchManager.GetProductDocIdSet(Query);
                }
                return _docIdSet;
            }
        }

        public IList<Document> ProductDocuments
        {
            get { return _productDocuments; }
        }

        public IList<SpecificationAttributeOption> Facets
        {
            get
            {
                if(_facets == null)
                {
                    _facets = new List<SpecificationAttributeOption>();
                    //var expectedFactes = _searchManager.GetFacetsForCategory(_filterContext.Categories[0]);
                    //_facets = _searchManager.GetActualFacets(_query, expectedFactes).FacetsGroup();
                }
                return _facets;
            }
        }

        public IList<PriceRangeValue> GetPriceRanges(IList<PriceRange> ranges)
        {
            if (ranges.Count > 0)
            {
                return _searchManager.GetPriceRanges(this.Query, ranges);
            }
            return new List<PriceRangeValue>();
        }


        public FilterContext FilterContext
        {
            get { return _filterContext; }
        }
    }
}
